cmake_minimum_required(VERSION 3.14)

project(DrillingrigCore LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core SerialBus SerialPort Qml)
set(CURL_INCLUDE_DIR)
set(CURL_LIBRARY)

find_package(CURL CONFIG REQUIRED)

if(CURL_FOUND)
  message(STATUS "Curl Found: ${CURL_VERSION_STRING} ${CURL_LIBRARIES} ${CURL_LINK_LIBRARIES}!")
else(CURL_FOUND)
  message(FATAL_ERROR "Could not find the CURL library and development files.")
endif()
#qt_standard_project_setup()

set(PLUGINS_MANAGER_FILES
  ../../include/PluginsAction.h
  Plugins/PluginsManager.h
  ../../include/PluginObject.h
  Plugins/PluginsManager.cpp
)

set(MEDIATOR_FRAMEWORK_FILES
    Frameworks/AbstruactEventMediator.h
    Frameworks/EquipmentAction.h
    Frameworks/DrillingrigEvent.h
    Frameworks/DrillingrigMediator.h
    Frameworks/MediatorEventDefine.h
    Frameworks/EventMediator.cpp
    Frameworks/DrillingrigEvent.cpp
    Frameworks/DrillingrigMediator.cpp
)

qt_standard_project_setup()



qt_add_library(DrillingrigCore SHARED
  Drillingrig_global.h
  Drillingrig.cpp
  Drillingrig.h
  ModbusClientProxy.h
  ModbusClientProxy.cpp
  ControlBridge.h
  ControlBridge.cpp
  ../Shared/ModbusEvent.h
  ../Shared/ModbusClient.h
  ../Shared/ModbusClient.cpp
  ${PLUGINS_MANAGER_FILES}
  ${MEDIATOR_FRAMEWORK_FILES}
 DrillingrigQmlEvent.h 
 DrillingrigQmlEvent.cpp)

if(APPLE)
    set(executable_path "$<TARGET_FILE_NAME:DrillingrigCore>.app")
else()
    set(executable_path "\${QT_DEPLOY_BIN_DIR}/$<TARGET_FILE_NAME:DrillingrigCore>")
endif()
target_link_libraries(DrillingrigCore 
    PRIVATE Qt${QT_VERSION_MAJOR}::Core
    PRIVATE Qt${QT_VERSION_MAJOR}::Qml
    PRIVATE Qt${QT_VERSION_MAJOR}::SerialBus
    PRIVATE Qt${QT_VERSION_MAJOR}::SerialPort
    PRIVATE CURL::libcurl
)

target_include_directories(DrillingrigCore
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)

target_compile_definitions(DrillingrigCore PRIVATE DRILLINGRIG_LIBRARY)

set(PLUGINS_MANAGER_HEADERS)
set(PLUGINS_MANAGER_SOURCES)
foreach(SOURCE_FILE ${PLUGINS_MANAGER_FILES})
  get_filename_component(SOURCE_NAME ${SOURCE_FILE} NAME)
  if(${SOURCE_NAME} MATCHES "^.*\\.(h|H)$")
    list(APPEND PLUGINS_MANAGER_HEADERS ${SOURCE_FILE})
  elseif(${SOURCE_NAME} MATCHES "^.*\\.(cpp|cxx)$")
    list(APPEND PLUGINS_MANAGER_SOURCES ${SOURCE_FILE})
  endif()
endforeach()

set(MEDIATOR_FRAMEWORK_HEADERS)
set(MEDIATOR_FRAMEWORK_SOURCES)
foreach(SOURCE_FILE ${MEDIATOR_FRAMEWORK_FILES})
  get_filename_component(SOURCE_NAME ${SOURCE_FILE} NAME)
  if(${SOURCE_NAME} MATCHES "^.*\\.(h|H)$")
    list(APPEND MEDIATOR_FRAMEWORK_HEADERS ${SOURCE_FILE})
  elseif(${SOURCE_NAME} MATCHES "^.*\\.(cpp|cxx)$")
    list(APPEND MEDIATOR_FRAMEWORK_SOURCES ${SOURCE_FILE})
  endif()
endforeach()



install(TARGETS DrillingrigCore
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

#message("root dir at " ${CMAKE_SOURCE_DIR})
#qt_generate_deploy_script(
#    TARGET DrillingrigCore
#    OUTPUT_SCRIPT deploy_core
#    CONTENT "
#qt_deploy_runtime_dependencies(
#    EXECUTABLE \"${CMAKE_SOURCE_DIR}/${CMAKE_INSTALL_BINDIR}/${executable_path}\"
#    NO_APP_STORE_COMPLIANCE
#)")
#install(SCRIPT ${deploy_core})

#qt_generate_deploy_app_script(
#    TARGET DrillingrigCore
#    OUTPUT_SCRIPT deploy_core
#    NO_UNSUPPORTED_PLATFORM_ERROR
#)
#install(SCRIPT ${deploy_core})

source_group("Plugins/Headers" FILES ${PLUGINS_MANAGER_HEADERS})
source_group("Plugins/Sources" FILES ${PLUGINS_MANAGER_SOURCES})
source_group("Framework/Headers" FILES ${MEDIATOR_FRAMEWORK_HEADERS})
source_group("Framework/Sources" FILES ${MEDIATOR_FRAMEWORK_SOURCES})

set_property(TARGET DrillingrigCore PROPERTY FOLDER "Core")
